package top.cflwork.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import top.cflwork.vo.BuyVo;
import top.cflwork.service.BuyService;
import top.cflwork.controller.BaseController;
import top.cflwork.util.PageUtils;
import top.cflwork.util.Query;
import top.cflwork.util.R;
import com.xiaoleilu.hutool.date.DateUtil;
/**
 * 购买表
 *
 * @author cfl
 * @email 275300091@qq.com
 * @date 2020-04-25 21:34:14
 */

@Controller
@RequestMapping("/buy")
public class BuyController extends BaseController {
    @Autowired
    private BuyService buyService;

    @GetMapping("buyPage")
    @RequiresPermissions("buy:buyPage")
    public String Buy() {
        return "/buy/buy";
    }

    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions("buy:list")
    public PageUtils list(BuyVo buyVo) {
        List<BuyVo> buyList = buyService.list(buyVo);
        Long total = buyService.count(buyVo);
        PageUtils pageUtils = new PageUtils(buyList, total);
        return pageUtils;
    }

    @ResponseBody
    @PostMapping("/listAll")
    @RequiresPermissions("buy:list")
    public List<BuyVo> listAll(BuyVo buyVo) {
        //查询列表数据
        List<BuyVo> buyList = buyService.list(buyVo);
        return buyList;
    }

    @GetMapping("/add")
    @RequiresPermissions("buy:add")
    public String add() {
        return "/buy/add";
    }

	/*@GetMapping("/edit/{id}")
	@RequiresPermissions("buy:edit")
	public String edit(@PathVariable("id") String id,Model model){
		BuyVo buy = buyService.get(id);
		model.addAttribute("buy", buy);
	    return "/buy/edit";
	}*/

    @GetMapping("/edit/{id}")
    @RequiresPermissions("buy:edit")
    @ResponseBody
    public BuyVo edit(@PathVariable("id") String id) {
        BuyVo buy = buyService.get(id);
        return buy;
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    public R save(@RequestBody BuyVo buy) {
        buy.setAdminId(getUser().getUserId());
        buy.setAdminName(getUser().getUsername());
        buy.setCreateTime(DateUtil.now());
        if (buyService.save(buy) > 0) {
            return R.ok("新增成功");
        }
        return R.error("新增失败");
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("buy:update")
    public R update(BuyVo buy) {
        buyService.update(buy);
        return R.ok("修改成功");
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("buy:remove")
    public R remove(String id) {
        if (buyService.remove(id) > 0) {
            return R.ok("删除成功");
        }
        return R.error("删除失败");
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("buy:batchRemove")
    public R remove(@RequestParam("ids[]") String[] ids) {
        buyService.batchRemove(ids);
        return R.ok("批量删除成功");
    }

    /**
     * 删除
     */
    @PostMapping("/batchSave")
    @ResponseBody
    @RequiresPermissions("buy:batchSave")
    public R batchSave(List<BuyVo> buyList) {
        buyService.batchSave(buyList);
        return R.ok("批量新增成功");
    }


}
